package com.allenkerr.solr;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by 小柯(Allen Kerr) on 17.5.1 21:12.
 */
public class SolrJUtils {

    private static String solrServerAddress;

    /*static {
        InputStream is = SolrJUtils.class.getClassLoader().getResourceAsStream("config.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        solrServerAddress = properties.getProperty("solr4_server_address");
    }*/

    private static class SolrServerHolder {
        static {
            InputStream is = SolrJUtils.class.getClassLoader().getResourceAsStream("config.properties");
            Properties properties = new Properties();
            try {
                properties.load(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            solrServerAddress = properties.getProperty("solr_server_address");
        }
        public static final SolrServer SOLR_SERVER = new HttpSolrServer(solrServerAddress);
    }

    //单例的实现方式：来自《Effective Java 1》的方法 ——> 静态内部类
    public static SolrServer getSolrServer() {
        return SolrServerHolder.SOLR_SERVER;
    }

}
